package org.ricks.gateway.ws;

import org.ricks.ioc.anno.Bean;
import org.ricks.ioc.anno.Value;
import org.ricks.net.http.server.HttpBootstrap;
import org.ricks.net.http.server.handler.WebSocketRouteHandler;

@Bean
public class WebSocketStarter {

    @Value("gateway.external.port")
    private int gatewayExternalPort;

    /**
     * websocket 启动入口
     */
    public void start () {

        //1. 实例化路由Handle
        WebSocketRouteHandler routeHandle = new WebSocketRouteHandler();

        //2. 指定路由规则以及请求的处理实现
        routeHandle.route("/", new WSHandler());

        // 3. 启动服务
        HttpBootstrap bootstrap = new HttpBootstrap();
        bootstrap.configuration().setWsIdleTimeout(5000);
        bootstrap.webSocketHandler(routeHandle);
        bootstrap.setPort(gatewayExternalPort);
        bootstrap.start();
    }
}
